# -*- coding: utf-8 -*-
import scrapy


class ZdmSpider(scrapy.Spider):
    name = 'zdm'
    allowed_domains = ['smzdm.com']
    start_urls = ['https://m.smzdm.com/youhui/p1/']
    num = 1

    def parse(self, response):
        # 验证代码是否运行
        # print('-------->', response)
        li_list = response.xpath('//*[@id="__layout"]/div/div[1]/div[1]/ul/li')

        for li_temp in li_list:
            title = li_temp.xpath('normalize-space(./a/div/div[2]/div[1]/text())').extract_first()
            price = li_temp.xpath('normalize-space(./a/div/div[2]/div[2]/text())').extract_first()
            time = li_temp.xpath('normalize-space(./a/div/div[2]/div[3]/div[1]/span/span[2])').extract_first()
            sites = li_temp.xpath('normalize-space(./a/div/div[2]/div[3]/div[1]/span/span[1])').extract_first()
            print(title, price, time, sites)
            yield {
                "title": title,
                "price": price,
                "time": time,
                "sites": sites
            }
        self.num += 1
        # 一页为20条数据，50页为1000条
        if self.num <= 50:
            # print(self.page_num)
            next_page_url = "https://m.smzdm.com/youhui/p%s/" % self.num
            # 生成Request，爬取下一页的数据
            yield scrapy.Request(url=next_page_url, callback=self.parse)
